package com.cloud.job.controller;

import com.cloud.job.service.QuartzJobService;
import com.cloud.job.util.ApiResult;
import com.cloud.job.util.MapUtil;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @packageName: com.cloud.job.controller
 * @name: JobController
 * @description:
 * @author: 董定卓
 * @dateTime: 2019/7/16 16:45
 */
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    QuartzJobService service;

    /**
     * 添加任务
     *
     * @param map
     * @return
     */
    @PostMapping("/add")
    public ApiResult add(@RequestParam Map<String, Object> map) throws Exception {
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return ApiResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return ApiResult.failResult("任务组名称不能为空");
        }
        String cronExp = MapUtil.getString(map, "cronExp");
        if (cronExp == null) {
            return ApiResult.failResult("cron表达式不能为空");
        }
        CronTriggerImpl trigger = new CronTriggerImpl();
        try {
            trigger.setCronExpression(cronExp);
            Date date = trigger.computeFirstFireTime(null);
            if (date == null || date.before(new Date())) {
                return ApiResult.failResult("cron表达式格式错误");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.failResult("cron表达式格式错误");
        }
        Map<String, Object> paramMap = null;
        Object param = map.get("param");
        if (param != null) {
            paramMap = (Map<String, Object>) param;
        }
        Class<?> clazz = Class.forName("com.cloud.job.job." + groupName+"Job");
        service.addJob(clazz, name, groupName, cronExp, paramMap);
        return ApiResult.successResult();
    }


    /**
     * 暂停任务
     *
     * @param map
     * @return
     */
    @PostMapping("/pause")
    public ApiResult pause(@RequestParam Map<String, Object> map) {
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return ApiResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return ApiResult.failResult("任务组名称不能为空");
        }
        service.pauseJob(name, groupName);
        return ApiResult.successResult();
    }

    /**
     * 恢复任务
     *
     * @param map
     * @return
     */
    @PostMapping("/resume")
    public ApiResult resume(@RequestParam Map<String, Object> map) {
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return ApiResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return ApiResult.failResult("任务组名称不能为空");
        }
        service.resumeJob(name, groupName);
        return ApiResult.successResult();
    }

    /**
     * 更新任务
     *
     * @param map
     * @return
     */
    @PostMapping("/update")
    public ApiResult update(@RequestParam Map<String, Object> map) {
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return ApiResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return ApiResult.failResult("任务组名称不能为空");
        }
        String cronExp = MapUtil.getString(map, "cronExp");
        Map<String, Object> paramMap = null;
        Object param = map.get("param");
        if (param != null) {
            paramMap = (Map<String, Object>) param;
        }
        service.updateJob(name, groupName, cronExp, paramMap);
        return ApiResult.successResult();
    }

    /**
     * 删除任务
     *
     * @param map
     * @return
     */
    @PostMapping("/del")
    public ApiResult del(@RequestParam Map<String, Object> map) {
        String name = MapUtil.getString(map, "name");
        if (name == null) {
            return ApiResult.failResult("任务名称不能为空");
        }
        String groupName = MapUtil.getString(map, "groupName");
        if (groupName == null) {
            return ApiResult.failResult("任务组名称不能为空");
        }
        service.deleteJob(name, groupName);
        return ApiResult.successResult();
    }

    /**
     * 启动所有任务
     *
     * @param map
     * @return
     */
    @PostMapping("/startAll")
    public ApiResult startAll(@RequestParam Map<String, Object> map) {
        service.startAllJobs();
        return ApiResult.successResult();
    }

    /**
     * 停止所有任务
     *
     * @param map
     * @return
     */
    @PostMapping("/shutdownAll")
    public ApiResult shutdownAll(@RequestParam Map<String, Object> map) {
        service.shutdownAllJobs();
        return ApiResult.successResult();
    }

    /**
     * 任务分页
     *
     * @param map
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/page")
    public ApiResult page(@RequestParam Map<String, Object> map, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "page", defaultValue = "1") int page) {
        List<Map<String, Object>> data = service.list(map, limit, page);
        long count = service.count(map);
        return ApiResult.successResultForPage(data, count);
    }

}
